import * as TYPES from "./../action-type";
let initial = {
  data: [],
  searchData: [],
};

const shopReducer = (state = initial, action) => {
  state = { ...state };
  if (action.type === TYPES.SET_SHOP_ALL_DATA) {
    state.data = action.data;
    state.searchData = [...action.data];
  }

  if (action.type === TYPES.DELETE_SHOP) {
    state.searchData = state.searchData.filter((item) => item.id != action.id);
  }

  if (action.type === TYPES.ADD_SHOP) {
    state.searchData = [action.data, ...state.searchData];
  }

  if (action.type === TYPES.EDIT_SHOP) {
    state.searchData = state.searchData.map((item) => {
      if (item.id == action.id) {
        return action.data;
      }
      return item;
    });
  }

  if (action.type === TYPES.SEARCH_SHOP) {
    if (!action.keyword) {
      state.searchData = [...state.data];
    } else {
      state.searchData = state.data.filter((item) =>
        item.name.includes(action.keyword)
      );
    }
  }

  return state;
};

export default shopReducer;
